package visualcbir.decorators;

import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import visualcbir.PnlView;
import visualcbir.View;

/**
 *
 * @author sheila
 */
public class FunZoom extends Functionality{
    private JPanel pnl;
    private JLabel lbl;
    private JSlider sld;
    
    double zoomScale;

    public FunZoom(View oIView, PnlView oPnlView){
        super(oIView, oPnlView);
        initComponents();
    }

    public void initComponents(){
        this.pnl=new JPanel();
        this.sld=new JSlider();
        this.lbl=new JLabel();
        this.pnl.setLayout(new java.awt.BorderLayout());

        lbl.setText("Zoom: 100%");
        this.pnl.add(lbl, java.awt.BorderLayout.NORTH);

        sld.setValue(100);
        sld.setMajorTickSpacing(50);
        sld.setMaximum(300);
        sld.setMinimum(50);
        sld.setMinorTickSpacing(10);
        sld.setPaintTicks(true);
        sld.setPaintLabels(true);
	sld.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(javax.swing.event.ChangeEvent evt)
            {
                lbl.setText("Zoom: " + sld.getValue() + "%");		
		if(sld.getValue() == 100) // 100%, fit available space
                        zoomScale=1;		
		else{
			float scale = sld.getValue() / 100.0f;
                        System.out.println("scale -->"+scale);
                        Dimension d = oView.getViewDimension();
			oView.setViewDimension(Math.round(d.width*scale),Math.round(d.height*scale));
                        zoomScale=scale;
		}
                //oView.setZoomScale(zoomScale);
                oView.setScaleFactor(zoomScale);
		//jsp.setPreferredSize(d);                
		//jsp.revalidate();
                //Fire a listener here

                oPnlView.setHasComponentChanged(true);
                
                //**********RECOMPUTE E REGENERATE POINTS***********************
                oView.update(null, null);// con este update diminui a tremedeira quando se redimensiona. //CLAVE AQUI. PORQUE AFETA ESTE UPDATEEEE E REDUZ MUITO A TREMEDEIRA?
                //a tremedeira tmb tem a ver com a funcao ComponentUtils.recomputeComponentDimensionsByView
                //**************************************************************
                oPnlView.repaint();		
            }
        });
        this.pnl.add(sld, java.awt.BorderLayout.CENTER);       
    }

    public void removeAll(){
        this.pnl.removeAll();
    }

    public JPanel getPnl() {
        return pnl;
    }

}
